<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <img src="../assets/img/logo.png" alt="" />
      <h2>
        梦天后台管理系统
      </h2>
      <el-button @click="exitLogin" class="exit" type="info">
        <i class="el-icon-s-fold"></i>
        退出
      </el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏区域  -->
      <el-aside :width="asideWidth">
        <el-menu
          :default-active="currentIndex"
          class="el-menu-vertical-demo"
          background-color="#434346"
          text-color="#409EFF"
          active-text-color="#7fff00"
          :uniqueOpened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
        >
          <!-- 控制侧边栏伸缩 -->
          <div class="flexibleDiv" @click="flexibleMenu">
            <i :class="flexibleFlag ? flexibleIcon[0] : flexibleIcon[1]"></i>
          </div>

          <!-- 侧边栏主体内容 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in asideMenu"
            :key="item.id"
          >
            <template slot="title">
              <i :class="menuIcon[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + i.path"
              v-for="i in item.children"
              :key="i.id"
              @click="saveNavState('/' + i.path)"
            >
              <i class="el-icon-menu"></i>
              {{ i.authName }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <!-- 放置一个路由占位符 -->
        <!-- 在main区域通过前端路由，实现单页面切换 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      asideMenu: [],
      menuIcon: {
        125: "iconfont icon-user-group-fill",
        103: "iconfont icon-databaseset-fill",
        101: "iconfont icon-service_fill",
        102: "iconfont icon-laptop-check",
        145: "iconfont icon-chart-line",
      },
      currentIndex: "", // 当前所在的菜单索引值
      flexibleFlag: true, //控制伸缩图标
      flexibleIcon: ["el-icon-s-fold", "el-icon-s-unfold"],
      isCollapse: false,
      asideWidth: "200px", // 动态绑定侧边栏宽度大小 ,默认为200px
    };
  },
  created() {
    this.getAsideMenu();
    this.currentIndex = window.sessionStorage.getItem("activePath");
  },

  methods: {
    exitLogin() {
      // 点击退出按钮，清空token，并跳转到登录页面
      this.$confirm("是否退出当前用户?", "退出登录", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$message.success("安全退出登录");
        window.sessionStorage.clear();
        this.$router.push("/login");
      });
    },

    // 获取左侧菜单栏数据
    async getAsideMenu() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200)
        return this.$message.console.error("获取菜单列表异常");
      this.asideMenu = res.data;
    },

    //控制侧边栏伸缩
    flexibleMenu() {
      // 切换图标
      this.flexibleFlag = !this.flexibleFlag;
      // 切换伸缩状态
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse) {
        this.asideWidth = "60px";
      } else this.asideWidth = "200px";
    },

    //  保存当前菜单的路径值
    saveNavState(path) {
      window.sessionStorage.setItem("activePath", path);
      this.currentIndex = window.sessionStorage.getItem("activePath");
    },
  },
};
</script>

<style scoped>
.iconfont {
  width: 24px;
  text-align: center;
  font-size: 16px;
  margin-right: 10px;
  vertical-align: middle;
  color: #c5e66d;
  margin-left: -2px;
}
a {
  text-decoration: none;
  color: #ffefbc;
}
.el-menu {
  border: none;
}
.el-container {
  height: 100%;
}
.el-header {
  position: relative;
  line-height: 56px;
  background-color: #4f5355;
  color: #ffefbc;
}
.el-header img {
  float: left;
  height: 100%;
  margin-right: 10px;
}
.el-header .exit {
  width: 85px;
  position: absolute;
  top: 10px;
  right: 35px;
  font-size: 16px;
}

.el-aside {
  background-color: #434346;
}
.el-main {
  background-color: #eaedf1;
}
.el-icon-s-fold {
  font-size: 16px;
}
.flexibleDiv {
  width: 100%;
  height: 40px;
  color: #fff;
  cursor: pointer;
}
.flexibleDiv:hover {
  background-color: #363638;
  color: #7fff00;
}
.flexibleDiv i {
  float: right;
  font-size: 20px;
  line-height: 40px;
  margin-right: 25px;
}
</style>
